<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\DoctorService;
use Illuminate\Http\Request;

class DoctorController extends BaseController
{
    //医生

    public function __construct(DoctorService $service)
    {
        $this->db = $service;
    }

    //选项
    public function getOption()
    {
        $res = $this->db->getOption();
        return $this->toJson(0, '', $res);
    }

    //列表
    public function getList(Request $request)
    {
        $pas['dname'] = $request->post('doc_name', ''); //医生名称
        $pas['seek_id'] = $request->post('seek_id', '');
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('limit', 10);
        $res = $this->db->getList($pages, $pas);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //申请
    public function apply(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        if (!$pas['uid']) {
            return $this->toJson(-2, '还没登录');
        }
        $pas['zyzgz'] = $request->post('zyzgz', ''); //职业资格证
        $keshi = $request->post('keshi', ''); //科室
        $yiyuan = $request->post('yiyuan', ''); //在职医院
        $pas['depart'] = $this->db->cateIdByName($keshi);
        if (!$pas['depart']) {
            return $this->toJson(-2, '科室不存在');
        }
        $pas['hospital'] = $this->db->hospitalIdByName($yiyuan);
        if (!$pas['hospital']) {
            return $this->toJson(-2, '医院不存在');
        }
        $res = $this->db->apply($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //代办列表
    public function daibanList(Request $request)
    {
        $token = $request->post('token', '');
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $res = $this->db->daibanList($uid, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //代办详情
    public function daibanOne(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        $id = $request->post('id', '');
        $res = $this->db->daibanOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }
}
